//
//  ActivityDetailsVC.m
//  豆瓣
//
//  Created by lanou3g on 15/12/11.
//  Copyright © 2015年 HaiTeng. All rights reserved.
//

#import "ActivityDetailsVC.h"
#import "LandingVC.h"
#import "UserDetailVC.h"
#import "DataBase.h"
#import "HTDataBase_DB.h"
#import "Model_DB.h"

@interface ActivityDetailsVC ()
@property(nonatomic,strong)UIAlertController *alertController;



@end

@implementation ActivityDetailsVC



- (void)loadView
{
    self.activityDetailsView = [[ActivityDetailsView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.view = self.activityDetailsView;
}



- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self setNavigationBarItem]; //设置导航栏
    self.activityDetailsView.model_activity_ = self.model_detail;

  
    
}



//设置导航栏
- (void)setNavigationBarItem
{
    
    self.navigationItem.title = self.model_detail.title;
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"btn_nav_back"] style:UIBarButtonItemStylePlain target:self action:@selector(leftAction:)];
    [self.navigationItem.leftBarButtonItem setTintColor:[UIColor whiteColor]];
    
     self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"btn_nav_share"] style:UIBarButtonItemStylePlain target:self action:@selector(rightAction:)];
    [self.navigationItem.rightBarButtonItem setTintColor:[UIColor whiteColor]];
    

}
#pragma mark 导航栏按钮事件
- (void)leftAction:(UIBarButtonItem *)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

#warning 数据存储.........
- (void)rightAction:(UIBarButtonItem *)sender
{

     LandingVC *land = [LandingVC new];
    UINavigationController *navigation_landing = [[UINavigationController alloc] initWithRootViewController:land];
    [navigation_landing.navigationBar setTintColor:nil];
    [navigation_landing.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
    land.hidesBottomBarWhenPushed = YES;
    navigation_landing.navigationBar.translucent= NO;
    
    
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString *State = [user objectForKey:@"State"];
    


    
    if (![State isEqualToString:@"YES"]) {
        
        
     [self showDetailViewController:navigation_landing sender:nil];
        
        
        
    }else{

        
//        [[DataBase shareDataBaseHandle] openDB];
//        NSMutableArray *array = [[DataBase shareDataBaseHandle] selectStudent];
//    
//        NSMutableArray *array_title = [NSMutableArray array];
//        for (ActivityModel *model1 in array) {
//            [array_title addObject:model1.title];
//        }
        [[HTDataBase_DB shareData] createDataBase];
        
        Model_DB *model_db = [[Model_DB alloc] init];
        model_db.title = self.model_detail.title;
        Model_DB *tempModel = [[[HTDataBase_DB shareData] selectModel:model_db Table:@"Activity"] lastObject];
        
       if (tempModel != nil) {
            UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"提示" message:@"该活动已经被收藏过" preferredStyle:UIAlertControllerStyleAlert];
            
            
            [alertC addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:nil]];
            
            
            [self presentViewController:alertC animated:YES completion:nil];
        }else{
            
//            NSUserDefaults *user_Activy_Model = [NSUserDefaults standardUserDefaults];
//            [user_Activy_Model setObject:self.model_detail.title forKey:self.model_detail.title];
            
            self.alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"收藏成功" preferredStyle:UIAlertControllerStyleAlert];
            
//             [[DataBase shareDataBaseHandle] insertActivity:self.model_detail];
            [[HTDataBase_DB shareData] insertModel:model_db Table:@"Activity"];
            
            
            [self presentViewController:self.alertController animated:YES completion:nil];
            [self.alertController dismissViewControllerAnimated:YES completion:nil];
//            //定时器,1秒后 警告视图消失
//            [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(performDismiss:) userInfo:nil repeats:NO];
            
        }
        
//        [[DataBase shareDataBaseHandle] closeDB]; 
        

    }
  
  
}
////定时器,1秒后 警告视图消失
//- (void)performDismiss:(NSTimer *)timer
//{
//    [self.alertController dismissViewControllerAnimated:YES completion:nil];
//}


@end
